package guoan.web.weixin;

import guoan.domain.Words;
import guoan.domain.WordsConfig;
import guoan.service.WordsConfigService;
import guoan.service.WordsService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/weixin/words")
public class WinXinWordsController {
	protected final Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private WordsConfigService wordsConfigService;
	@Autowired
	private WordsService wordsService;

	/**
	 * 跳转到不同的内容页面
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(@RequestParam(value = "num", required = true, defaultValue = "1") int num, Model model) {
		Page<Words> page = wordsService.findPage(num);
		model.addAttribute("totalCounts", page.getTotalElements());
		model.addAttribute("currentPage", page.getNumber()+1);
		model.addAttribute("data", page.getContent());
		WordsConfig wordsConfig = wordsConfigService.initOne();
		model.addAttribute("wordsConfig", wordsConfig);
		return "/weixin/words/index";
	}

	/** 查看 */
	@RequestMapping(value = { "toAdd" }, method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/weixin/words/toAdd";
	}

	/** 查看 */
	@RequestMapping(value = { "doAdd" }, method = RequestMethod.POST)
	public String doAdd(Words words, Model model) {
		Preconditions.checkNotNull(words, "参数错误");
		wordsService.save(words);
		model.addAttribute("msg", "操作成功，请等待界面跳转");
		model.addAttribute("url", "/weixin/words/index");
		return "/weixin/msg";
	}
}
